<?php
namespace Home\Controller;
use Common\Controller\MyInitController;
class HomeworkController extends MyInitController {
	private $peerfile = "";
    public function index($itemid = '',$type = '',$backurl = ''){
		
		$Homework 	= 	D("homework");
		$List		=	D("list");
		$Peer		=	D("peer");
		
		
		$openid 	= 	$this->openid;
		$current	=	date('Y-m-d H:i:s', time());
		$list		=	$List->where("isdelete = 0 ")->field("id,ex_name")->select();
		$list_menu	=	$List->where("isdelete = 0 AND updatastart< '$current' AND resubmitend>'$current' ")->field("id,ex_name,updataend")->select();
		
		for($i=0;$i<count($list_menu);$i++){
			($list_menu[$i]['updataend']<$current) && $list_menu[$i]['ex_name'] .="(补交)";	
		}
		//var_dump(array_keys($list));
		foreach($list as $v){						
			$tmp 	= 	$Homework->where("openid = '{$openid}' AND homeworknum = {$v['id']} 
											AND isdelete = 0")->order("delivertime desc")->find();
			$ispeer	=	$Peer->where("homeworknum = {$v['id']} AND openid ='{$openid}' AND isdelete = 0")->count();									
			if($tmp){
				if($score	=	$Peer->where("homeworknum = {$v['id']} 
												AND fileSaveName ='{$tmp['filesavename']}' 
												AND isdelete = 0")->find()){
					$tmp['score'] 	= 	$score['score'];
				}				
				$tmp['ex_name'] =	$v['ex_name'];								
				$tmp['ispeer'] 	=	$ispeer;
				$homework[] 	= 	$tmp;
			}	
		}				
		$this->assign("list",$list);
		$this->assign("list_menu",$list_menu);
		$this->assign("homework",$homework);
		
		$this->display();
    }
	public function upload($savePath='/Discussion/'){ //定义默认上传路径
		$Homework	 	= 	D("homework");	
		$List			=	D("list");		
		$homeworkNum 	= 	I('post.homeworkNum');
		$iscover		=	I("post.iscover");
		$current		=	date('Y-m-d H:i:s', time());
		if($homeworkNum	==	"") $this->error("请选择任务");
		
		$upload = new \Think\Upload();
		$upload->maxSize 		=	3145728;  //以字节为单位，这里设置最大为3M
		$upload->exts 			=	array('doc','docx');
		$upload->rootPath 		= 	'./Public/Upload'; // 设置附件上传根目录
		$upload->savePath 		= 	$savePath; // 设置附件上传（子）目录 在本地创建Upload目录
		$upload->subName  		= 	$homeworkNum;
		$upload->uploadReplace 	= 	TRUE;
		$info = $upload->upload();
		if(!$info){
			
			//return false;
			$this->error($upload->getError());
		}else{
			foreach($info as $file){
				//改变路径以移植sae
				$path = $file['savepath'].$file['savename'];
				$path = str_replace('/Upload/','',$path);
			}
		}

		
		$list		=	$List->where("id	= $homeworkNum AND isdelete = 0")->field("id,updataend,resubmitend")->find();
		if($list['updataend']<$current){
			$data["isresubmit"] = 1;
		}
		if($list["resubmitend"]<$current){
			$this->error("超过补交时间！");
		}
		
		$data["openid"] 		= 	I('post.openid');
		$data["homeworkNum"] 	= 	$homeworkNum;
		$isexist = $Homework->where($data)->find();
		if(!is_null($isexist)){
			if($iscover == 1){
				$Homework->where($data)->setField("isdelete",1);
			}else{
				$this->error("作业已存在！");
			}			
		}
		
		$data["size"] 	  		=	substr($_FILES['homeworkFile']['size']/1024,0,5).' Kb';
		$data["filename"] 		=	$_FILES['homeworkFile']['name'];		
		$data["fileSaveName"] 	= 	$path;
		
		
		
		if($Homework->add($data)){
			$this->success("添加成功！");
		}
		else{
			$this->error("添加失败！");
		}
	}
	public function peerResponse(){
		
		$Layout 	=  	A("Layout");
		$Layout->init($this->openid);
		
		$List		=	D("list");		
		$current	=	date('Y-m-d H:i:s', time());		
		$list		=	$List->where("isdelete = 0 AND responsestart<'$current' 
										AND responseend > '$current'")
							->field("id,
									ex_name,
									responsestart,
									responseend")->select();
		
		$this->assign("responselist",$list);
		$this->display();
	}
	public function getPeerDetail(){
		
		$Homework	=	D("homework");
		$Itemlist	=	D("itemlist");
		$Peer		=	D("peer");
		
		$openid 	= 	I('post.openid');
		$chapter	=	I("post.chapter");
		$ex_name	=	I("post.ex_name");
		if(!($openid&&$chapter&&$ex_name)){
			$this->error("缺少参数！");
		}
		$itemlist	=	$Itemlist->where("chapter = $chapter")->find();
		
		if($Peer->where("homeworkNum = $chapter AND openid ='{$openid}' AND isdelete = 0")->find()){
			echo "你已完成互评！";
			return false;
		}
		/*====获取被互评的作业==*/
		$files		=	$Homework->where("homeworknum = $chapter AND openid <>'{$openid}' AND isdelete = 0 AND ispeer = 0")->select();
		//$files			=	array_filter($files,"choosePeer");
		
		if(empty($files)){
			echo "尚未有其他上传者或上传的作业已被批改！";
			return false;
		}

		$count  	=  	count($files);		
		$row 		=  	rand(1,$count)-1;
		$filesavename	=	$files[$row]["filesavename"];
		
		/*====获取参考答案的url==*/
		$answers	=	"/kec/参考答案/参考答案_".$chapter.".docx";
		
		$this->assign("answers",$answers);
		$this->assign("list_verification",$itemlist['que1']);
		$this->assign("list_cloze_error_correction",$itemlist['que2']);
		$this->assign("list_programme",$itemlist['que3']);
		$this->assign("filesavename",$filesavename);
		$this->assign("openid",$openid);
		$this->assign("ex_name",$ex_name);
		$this->assign("chapter",$chapter);
				
		layout(false);
		
		$this->display();
	}
	public function setResponse(){
		
		$List		=	D("list");
		$Peer		=	D("peer");
		$Homework	=	D("Homework");
		
		$openid 	= 	I('post.openid');
		$chapter	=	I("post.chapter");
		$review		=	I("post.review");
		$filesavename	=	I("post.filesavename");
		
		$endtime	=	$List->find($chapter);
		$student	=	$Homework->where("filesavename = '{$filesavename}'")->find();
		//var_dump($student);
		//return false;
		$data["studentid"]	=	$student["openid"];
		$data["filename"]	=	$student["filename"];
		$data["homeworkNum"]=	$chapter;
		$data["openid"]		=	$openid;
		$data["fileSaveName"]		=	$filesavename;
		
		/*====是否超过时间==*/
		$current	=	date('Y-m-d H:i:s', time());
		if($current>$endtime["responseend"]){
			$this->error("超过时间!");
		}
		$data["responsetime"]	=	$current;
		
		/*====是否已被评价==*/
		$student["ispeer"] && $this->error("该作业已被评价！请重新选择作业！",U("Homework/peerResponse"));
		
		for($i	=	1;$i<=15;$i++){
			$data["score$i"]	=	I("post.score$i") ;
			$score				+=	$data["score$i"];
		}
		$data					=	array_merge($data,compact("score"));
		$data					=	array_merge($data,compact("review"));
		
		$savePath='/Response/';
		$upload = new \Think\Upload();
		$upload->maxSize 		=	3145728;  //以字节为单位，这里设置最大为3M
		$upload->exts 			=	array('doc','docx');
		$upload->rootPath 		= 	'./Public/Upload'; // 设置附件上传根目录
		$upload->savePath 		= 	$savePath; // 设置附件上传（子）目录 在本地创建Upload目录
		$upload->subName  		= 	$chapter;
		$upload->uploadReplace 	= 	TRUE;
		$info = $upload->upload();
		if(!$info){						
			$this->error($upload->getError());
		}else{
			foreach($info as $file){			
				$path = $file['savepath'].$file['savename'];
				$path = str_replace('/Upload/','',$path);
			}
		}
		$data["new_name"] 	= 	$path;
		
		$result		=	$Homework->where("filesavename = '{$filesavename}'")->setField("ispeer",1);
		$result		*=	$Peer->add($data);
		
		if($result){
			$this->success("添加成功！");			
		}
		else{
			$this->error("添加失败！");
		}					
	}
	public function choosePeer($arr){
		$arr['openid'] = $this->peerfile;
	}

 
	
	
}




?>